package com.example.minelocationdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.minedata.location.CoordType;
import com.minedata.location.MineLocation;
import com.minedata.location.MineLocationListener;
import com.minedata.location.MineLocationManager;
import com.minedata.location.MineLocationOptions;
import com.minedata.location.MineLocationTraceUploader;

public class DemoActivity extends AppCompatActivity {


    private TextView tv_show;
    private Button btn;
    private static String TAG = "DemoActivity";
    private String locationUrl = "https://service.minedata.cn/service/lbs/location/newposition/";
    private String AuthorizeUrl = "https://service.minedata.cn/service/lbs/location/";

    private String AuthorizeKey = "你申请到的key";

    private ProgressDialog waitDialog;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        tv_show = findViewById(R.id.tv_show);
        btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if ("".equals(AuthorizeKey) || "".equals(AuthorizeUrl)) {
                    Toast.makeText(DemoActivity.this, "请输入授权URL和Key", Toast.LENGTH_SHORT).show();

                    return;
                }

                initLocation();
                waitDialog = new ProgressDialog(DemoActivity.this);
                waitDialog.setTitle("定位中");
                waitDialog.setMessage("请等待。。。");
                waitDialog.setIndeterminate(true);
                waitDialog.setCancelable(false);
                waitDialog.show();

            }
        });


    }


    private void initLocation() {
        MineLocationManager.getInstance().setAgreePrivacy(true);
        MineLocationManager.getInstance().setOutLog(false);
        MineLocationManager.getInstance().init(getApplicationContext(), MineLocationManager.LocationSource.all);
        MineLocationManager.getInstance().addListener(new MineLocationListener() {
            @Override
            public void onSimLocationChanged(MineLocation mineLocation) {

            }

            @Override
            public void onLocationChanged(MineLocation mineLocation) {
                waitDialog.dismiss();


                sshwo(mineLocation.getProvider() + "纬度：" + mineLocation.getLatitude() + "，经度：" + mineLocation.getLongitude() + "时间:" + DateUtils.stampToDate(mineLocation.getTime() + "") + "\n");
                MineLocationManager.getInstance().removeAllListener();
                MineLocationManager.getInstance().stop();
                MineLocationManager.getInstance().cleanup();

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                Log.d("TAG", i + "");
                //如果是基站定位，这个是用来判断基站服务返回的状态的。

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        });


        MineLocationOptions options = new MineLocationOptions();
        options.setCoordType(CoordType.WGS84);
        options.setGpsInterval(1000);
        options.setNetWorkInterval(1000);
        options.setStationInterval(5000);
        options.setStationUrl(locationUrl);
        options.setAuthorizeUrl(AuthorizeUrl);
        options.setAuthorizeKey(AuthorizeKey);
        MineLocationManager.getInstance().start(options);


    }

    private void sshwo(String msg) {
        i++;
        String tt = tv_show.getText().toString();
        tv_show.setText(i + ":" + msg + tt);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        MineLocationTraceUploader.getInstance().stop();
    }
}